import {
    equipment
} from "@/api/factuer";
import {
    handleConfirm
} from "@/utils/messageBox";

export default {
    data() {
        return {
            remarkVisible: false,
            currentRow: {}, //当前行
            logVisible: false,

            selectedData: [], //列表选中数据
            // 标签
            tagsVisible: false,
            tagsSelectedData: [], //选中标签

            // 单个设备编辑标签
            tagSetVisible:false,
            tagSetData:[],
        }
    },
    methods: {
        isRemarkRow(row) {
            this.remarkVisible = true;
            this.currentRow = {
                ...row
            };
        },
        closeRemarkDialog() {
            this.remarkVisible = false;
            this.fetchData();
        },
        // 连接日志
        logDialog(row) {
            this.currentRow = {
                ...row
            };
            this.logVisible = true;
        },

        // 批量设置标签
        isSetTags() {
            if (this.selectedData.length == 0) {
                this.$message.error("请勾选要设置标签的设备~");
                return;
            }
            this.tagsVisible = true;
        },
        tagConfirmDialog() {
            if (this.tagsSelectedData.length == 0) {
                this.$message.error("请选择要设置的标签！");
                return;
            }
            handleConfirm("确定要批量设置标签吗？", "warning", "提示")
                .then((res) => {
                    this.doSetTags();
                })
                .catch((err) => {});
        },
        doSetTags() {
            equipment("/tag/batch/insert", "post", {
                equipIds: this.selectedData
                    .map((item) => {
                        return item.id;
                    })
                    .join(","),
                tagIds: this.tagsSelectedData
                    .map((item) => {
                        return item.id;
                    })
                    .join(","),
            }).then((res) => {
                if (res.code == 200) {
                    this.tagsVisible = false;
                    this.$message.success(res.message);
                    this.fetchData();
                }
            });
        },
        // 选中数据
        handleSelect(e) {
            this.selectedData = e;
        },
        handleTagsSelect(e) {
            this.tagsSelectedData = e;
        },
        tagSetConfirmDialog() {
            handleConfirm(`确定要${this.tagSetData.length==0 ?'重置':'设置'}标签吗？`, "warning", "提示")
                .then((res) => {
                    this.doTagSet();
                })
                .catch((err) => {});
        },

        tagSetShow(row) {
            this.tagSetVisible = true;
            this.currentRow = row;
            this.tagSetData = this.currentRow.tags?this.currentRow.tags.map((item) => {
                return item.id;
            }):[];
        },
        handleTagSetChange(e) {
            this.tagSetData = e;
        },

        doTagSet() {
            equipment(`/tag/insert/tag/${this.currentRow.id}`, 'post', {
                tagIds: this.tagSetData.join(',')
            }).then(res => {
                if (res.code == 200) {
                    this.tagSetVisible = false;
                    this.$message.success(res.message);
                    this.fetchData();
                }
            });
        },
    },
}
